package ar.com.mindset.geometrics.colorSchemes.continuous {
	import ar.com.mindset.geometrics.ColorEntry;
	import ar.com.mindset.geometrics.RegionData;
	import ar.com.mindset.geometrics.colorSchemes.ContinuousColorScheme;
	
	import com.degrafa.core.utils.ColorUtil;
	
	import mx.collections.ArrayCollection;

	public class LinealDivisionStrategy implements DivisionStrategy {

		public var divisionCount:Number=6;
		public var minColor:Object="#FFFFFF";
		public var maxColor:Object="#888888";	
		
		public function get defaultColor():Object{
			return maxColor;
		}

		public function getDivisions(colorScheme:ContinuousColorScheme, regionDataCollection:ArrayCollection, valuePropertyName:String):ArrayCollection{


			var min:Number;
			var max:Number;
			var xColor:uint=ColorUtil.resolveColor(minColor);
			var yColor:uint=ColorUtil.resolveColor(maxColor);

			var firstElement:Boolean=true;
			for each(var region:RegionData in regionDataCollection) {
				var element:Number=region[valuePropertyName] as Number;
				if (firstElement) {
					min=element;
					max=element;
					firstElement=false;
				} else {
					if (min > element) {
						min=element;
					}
					if (max < element) {
						max=element;
					}
				}
			}

			var distance:Number=(max - min) / divisionCount;

			var retVal:ArrayCollection=new ArrayCollection();

			var firstColor:*=ColorUtil.hexToRgb(xColor);
			var secondColor:*=ColorUtil.hexToRgb(yColor);

			var firstRed:Number=firstColor["red"];
			var firstGreen:Number=firstColor["green"];
			var firstBlue:Number=firstColor["blue"];

			var secondRed:Number=secondColor["red"];
			var secondGreen:Number=secondColor["green"];
			var secondBlue:Number=secondColor["blue"];

			for (var i:Number=1; i < divisionCount; i++) {
				var entry:ColorEntry=new ColorEntry();
				entry.key=min + (distance * i);

				var finalColor:String=avg(firstRed, secondRed, i - 1, divisionCount) + "," + avg(firstGreen, secondGreen, i - 1, divisionCount) + "," + avg(firstBlue, secondBlue, i - 1, divisionCount);

				entry.color=ColorUtil.resolveColorFromRGB(finalColor);

				retVal.addItem(entry);
			}
			return retVal;
		}
		
		private function avg(num1:Number, num2:Number, index:Number, total:Number):Number {
			return (num1 * (total - index) / total) + (num2 * index / total);
		}



	}
}